package com.gxuwz.core.util;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import com.google.gson.Gson;
import com.gxuwz.core.model.Cols;
import com.gxuwz.pojo.Chart;
import com.gxuwz.pojo.ChartData;
import com.gxuwz.pojo.DataEntity;
import com.gxuwz.pojo.Topic;

public class MainUtil {

	/**
	 * 判断字符串是否为数字
	 * @param str
	 * @return
	 */
	  public static boolean isInteger(String str) {  
	        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");  
	        return pattern.matcher(str).matches();  
	  }
	  
	  public static boolean noNull(String str){

		if (str != null && !"".equals(str.trim()) && str.trim().length() != 0) {
		   return true;
		}else{
			return false;
		}
	  }
	  
	  public static String getCols(List<Topic> topicList){
		  Gson gson = new Gson();
		  Cols col;
		  List<Cols> colList = new ArrayList<Cols>();
		  for(Topic t : topicList){
			  col = new Cols();
			  col.setField("data"+t.getId());
			  col.setTitle(t.getTopicName());
			  colList.add(col);
		  }
		// ,{fixed: 'right', title:'操作',  align:'center', toolbar: '#barDemo'}
			
		  col = new Cols();
		  col.setFixed("right");
		  col.setTitle("操作");
		  col.setAlign("center");
		  col.setToolbar("#barDemo");
		  colList.add(col);
		  
		  return gson.toJson(colList);
		  
	  }
	  
	  public static String getThead(List<Topic> topicList){
		  StringBuffer sb = new StringBuffer();
		  sb.append("<thead><tr>");
		  //隐藏id
		  sb.append("<th lay-data=\"{field:'id',hide:true}\"></th>");
		  for(Topic topic : topicList){
			  sb.append("<th lay-data=\"{field:'data"
			  		+ topic.getId()
			  		+ "', edit:'text'}\">"
			  		+ topic.getTopicName()
			  		+ "</th>");
		  }
		  sb.append("<th lay-data=\"{fixed: 'right',  align:'center', toolbar: '#barDemo'}\">操作</th>");
		  sb.append("</tr></thead>");
		  return sb.toString();
	  }
	  
	  public static String[] getSearchDatas(List<Topic> topicList, Map<String,String> map){
		  String[] s = new String[topicList.size()];
		  
		  for(int i=0;i<topicList.size();i++){
			  s[i] = map.get("data"+topicList.get(i).getId());
		  }
		  
		  return s;
	  }
	  
	  /**
	   * 获得搜索框
	   * @param topicList
	   * @return
	   */
	  public static String getSearch(List<Topic> topicList){
		  StringBuffer sb = new StringBuffer();
		  for(Topic topic : topicList){
			  
			  sb.append(" <div class='layui-inline'>");
			  sb.append(topic.getTopicName()+":");
			  if(topic.getTopicType() == 1 || topic.getTopicType() == 2){
				  sb.append("<input class='layui-input' name='data"
				  		+ topic.getId()
				  		+ "' id='data"
				  		+ topic.getId()
				  		+ "' autocomplete='off'>");
			  }else if(topic.getTopicType() == 3 || topic.getTopicType() == 4){
				  String[] options = topic.getOptions().split("；");
				  sb.append("<select id='data"
				  		+ topic.getId()
				  		+ "' name='data"
				  		+ topic.getId()
				  		+ "'  lay-search><option value=''></option>");
				  //添加选项
				  for(String option:options){
					  if(option != null && !"".equals(option)){
						  sb.append("<option value='"
						  		+ option
						  		+ "'>"
						  		+ option
						  		+ "</option>");
					  }
				  }
				  sb.append("</select>");
			  }
			  
			  sb.append("</div>");
		  }

		  return sb.toString();
	  }
	  
	  
	  /**
	   * 获得图表数据
	   * @param dataList
	   * @param topicList
	   * @return
	   */
	  public static List<Chart> getChart(List<DataEntity> dataList,List<Topic> topicList){
		  //图表
		  List<Chart> chartList = new ArrayList<Chart>();
		  //数量分布
		  List<HashMap<String,Integer>> list = new ArrayList<HashMap<String,Integer>>();
		  //基础信息，名字
		  for(Topic topic:topicList){
			  Chart chart = new Chart();
			  chart.setText(topic.getTopicName()+"-分布情况");
			  chart.setName(topic.getTopicName());
			  
			  chartList.add(chart);
			  list.add(new HashMap<String, Integer>());
		  }
		  //获得数据分布保存map中，<name,value>
		  
		  
		  for(DataEntity data:dataList){
			  //遍历数据
			  String[] s = data.getDatas();
			  for(int i=0;i<s.length;i++){
				  if(s[i] != null){
					  HashMap<String,Integer> map = list.get(i);
					  if(topicList.get(i).getTopicType() == 4){
						  //多选情况
						  String[] options = s[i].split("；");
						  for(String option:options){
							  if(map.get(option) == null){
								  map.put(option, 1);
							  }else{
								  map.put(option, map.get(option)+1);
							  }
						  }
					  }else{
						  if(map.get(s[i]) == null){
							  map.put(s[i], 1);
						  }else{
							  map.put(s[i], map.get(s[i])+1);
						  }
					  }

					  list.set(i, map);
				  }
			  }
		  }
		  
		  for(int i=0;i<list.size();i++){
			  Map<String,Integer> m = list.get(i);
			  //解析map保存到图表数据中
			  List<String> keys = new ArrayList<String>();
			  List<ChartData> chartData = new ArrayList<ChartData>();
		      for(Map.Entry<String, Integer> entry: m.entrySet()){
		    	keys.add(entry.getKey());
		    	ChartData c = new ChartData();
		    	c.setName(entry.getKey());
		    	c.setValue(entry.getValue());
		    	chartData.add(c);
		       }
		       Chart chart = chartList.get(i);
		       chart.setLegendData(keys);
		       chart.setCdList(chartData);
		       chartList.set(i, chart);
		  }
		  
		  return chartList;
	  }
	  
	  /**
	   * 获得选项map
	   * @param topicList
	   * @return
	   */
	  public static Map<String,String[]> getOptionMap(List<Topic> topicList){
		  Map<String,String[]> optionMap = new HashMap<String, String[]>();
		  for(Topic topic:topicList){
			  if(topic.getTopicType() == 3 || topic.getTopicType() == 4){
				  String[] options = topic.getOptions().split("；");
				  optionMap.put("key"+topic.getId(), options);
			  }
		  }
		  return optionMap;
	  }
	  
	  //计划a
//	  public static List<Chart> getChart(List<DataEntity> dataList,List<Topic> topicList){
//		  //图表
//		  List<Chart> chartList = new ArrayList<Chart>();
//		  //数量分布
//		  List<HashMap<String,Integer>> list = new ArrayList<HashMap<String,Integer>>();
//		  //基础信息，名字
//		  for(Topic topic:topicList){
//			  Chart chart = new Chart();
//			  chart.setText(topic.getTopicName()+"-分布情况");
//			  chart.setName(topic.getTopicName());
//			  
//			  chartList.add(chart);
//			  list.add(new HashMap<String, Integer>());
//		  }
//		  //获得数据分布
//		  //遍历填写记录
//		  for(DataEntity data:dataList){
//			  //遍历数据
//			  String[] s = data.getDatas();
//			  for(int i=0;i<s.length;i++){
//				  HashMap<String,Integer> map = list.get(i);
//				  if(map.get(s[i]) == null){
//					  map.put(s[i], 1);
//				  }else{
//					  map.put(s[i], map.get(s[i])+1);
//				  }
//				  list.set(i, map);
//			  }
//		  }
//		  
//		  for(int i=0;i<list.size();i++){
//			  Map<String,Integer> m = list.get(i);
//			  //解析map保存到图表数据中
//			  List<String> keys = new ArrayList<String>();
//			  List<Integer> values = new ArrayList<Integer>();
//		      for(Map.Entry<String, Integer> entry: m.entrySet()){
//		    	keys.add(entry.getKey());
//		        values.add(entry.getValue());
//		       }
//		       Chart chart = chartList.get(i);
//		       chart.setKeys(keys);
//		       chart.setValues(values);
//		       chartList.set(i, chart);
//		  }
//		  
//		  return chartList;
//	  }
}
